package com.example.myapplication;

import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.time.Instant;

public class MainActivity extends AppCompatActivity {

    private Button mBtnLogin,mBtnResigter;
    private EditText metid,metpass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnLogin=findViewById(R.id.btn_login);
        mBtnResigter=findViewById(R.id.btn_register);
        metid=findViewById(R.id.et_1);
        metpass=findViewById(R.id.et_2);
        String suc="登录成功";
        String fail="用户名或密码错误，请重试";
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2=null;
                intent2 = new Intent(MainActivity.this,ActActivity.class);
                startActivity(intent2);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent=null;
                        BufferedReader buf=null;
                        String result;
                        String id=metid.getText().toString();
                        String password=metpass.getText().toString();
                        HttpURLConnection logincon = null;
                        String url="http://10.128.48.203:8000/login?work_number="+id+"&password="+password;
                        try {
                            URL requesturl= new URL(url);
                            logincon=(HttpURLConnection)requesturl.openConnection();
                            logincon.setRequestMethod("GET");
                            logincon.setReadTimeout(5000);
                            logincon.connect();

                            if(logincon.getResponseCode()==200){
                                //获取二进制字节流
                                InputStream inputs=logincon.getInputStream();

                                //将二进制流包装
                                buf =new BufferedReader(new InputStreamReader(inputs));

                                //读取字符串
                                String line;
                                StringBuilder builder = new StringBuilder();
                                while((line=buf.readLine())!=null){
                                    builder.append(line);
                                    builder.append("\n");
                                }
                                result=builder.toString();
                                JSONObject jsonObject = new JSONObject(result);
                                String errno=jsonObject.optString("errno");
                                String oid=jsonObject.optString("oid");
                                switch (errno) {
                                    case "0":
                                        //Toast.makeText(getApplicationContext(),suc,Toast.LENGTH_SHORT).show();
                                        intent = new Intent(MainActivity.this,TestActivity.class);
                                        Bundle bundle =new Bundle();
                                        bundle.putString("name",id);
                                        bundle.putString("oid",oid);
                                        intent.putExtras(bundle);
                                        startActivity(intent);
                                        break;
                                    case "1":
                                        Toast toastfail=Toast.makeText(getApplicationContext(),fail,Toast.LENGTH_SHORT);
                                        toastfail.setGravity(Gravity.CENTER,0,0);
                                        toastfail.show();
                                        break;
                                }
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        mBtnResigter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=null;
                intent = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

    }
}